// void, Obj This

Unit newunit;
Barrack this;
point ptExit, ptEnter;
int perc, level, sum;
Query qEnemies;
str carCmdParam, cdrCmdParam; 

carCmdParam = ParseStr(cmdparam, cdrCmdParam); 

this = This.AsBarrack();

qEnemies = Intersect(ObjsInSight(.settlement.GetCentralBuilding(),"Object"),EnemyObjs(.player(),"Object"));
 
perc = 100 - EnvReadInt(.settlement, "BarrackTrainTimeDecrease");

sum = (.cmddelay * perc) / 100;

if(!.IsHeirOf("BaseTemple"))
	if(!.IsHeirOf("BaseArena")){
		EnvWriteString(.settlement, "last_train_unit", carCmdParam);
		EnvWriteInt(.settlement, "last_time_unit", sum);	
	}
		
//pr((.cmddelay * perc) / 100);
.Progress(sum);
// Create unit
ptExit = .GetExitPoint(.settlement.GetCentralBuilding.pos, false);
if(ptExit.x==-1 && ptExit.y==-1){
	Sleep(rand(100)+100);
	newunit = Place(carCmdParam, Point(0,0), this.player);
	if(newunit.IsValid)
		.settlement.AddUnit(newunit);
} else {
	Sleep(rand(100)+100);
	newunit = Place(carCmdParam, ptExit, this.player);
	if(newunit.IsValid){
		if(qEnemies.IsEmpty())
			newunit.SetCommand("enter", .settlement.GetCentralBuilding);
		else
			newunit.SetCommand("advanceenter", .settlement.GetCentralBuilding);
	}
}

// Set level
level = 1;

if(newunit.IsValid){
	if (newunit.IsHeirOf("BaseMage"))
	{
		if(!newunit.HasItem("Watch Eye I")){
			newunit.AddItem("Watch Eye I");
			if(newunit.HasItem("Watch Eye I")){
				Item item;
				item = newunit.FindItem("Watch Eye I");	
				if (item.IsValid) 
					item.SetUseCount(3);
			}
		}
		if(rand(100) < 3){
			if(!newunit.HasItem("Poison Mushroom"))
				newunit.AddItem("Poison Mushroom");
		}
		
	}
	if( GetClassRace(carCmdParam) == Carthage ){
		if (!newunit.IsHeirOf("BaseMage"))
		{	
			if( EnvReadString(.settlement, "Shared Strength") == "researched" ) {
				int nThisClass;
				nThisClass = Count(.player, carCmdParam);
				if( nThisClass > 12 )
					level = 6;
			}
			if( EnvReadString(.settlement, "Shared Triumph") == "researched" ) {
				int nThisClass;
				nThisClass = Count(.player, carCmdParam);
				if( nThisClass > 24 )
					level = 12;
			}
		}
	}
	{
		int l2;
		l2 = EnvReadInt(.settlement, "levels/" + carCmdParam);
		if (l2 > level) level = l2;
	}

	// standartization
	{
		if(!newunit.IsHeirOf("BaseMage"))
		{	
			int l2;
			l2 = EnvReadInt(.player, "standard_spawn_level");
			if (l2 > level) level = l2;
		}
	}
	 
	newunit.SetLevel(level);

	if (!newunit.IsHeirOf("BaseMage")){
		if(!.IsHeirOf("TBarracks")){
			if( EnvReadString(.settlement, "equip_iron") != "on") {
				if(EnvReadInt(.player, "resources/Iron") > 0){
					if(!newunit.HasItem("Steel weapon and armor")){
						newunit.AddItem("Steel weapon and armor");
						EnvWriteInt(.player, "resources/Iron", EnvReadInt(.player, "resources/Iron") - 1);
					}
				}
			}
		}
		if(newunit.IsHeirOf("EGuardian")){
			if(!newunit.HasItem("Large Shield"))
				newunit.AddItem("Large Shield"); 
		}
		if(EnvReadString(.player, "army/CountStr") == "H!"){
			if(!newunit.HasItem("Status of the Horde!")){
				newunit.AddItem("Status of the Horde!");
			}
		}
		if (EnvReadInt(.settlement, "items/Bear teeth amulet") == 1)
			newunit.AddItem("Bear teeth amulet");
		if (EnvReadInt(.settlement, "items/Herb amulet of luck") == 1)
			newunit.AddItem("Herb amulet of luck");
		if (EnvReadInt(.settlement, "items/Belt of might") == 1)
			newunit.AddItem("Belt of might");
		if (EnvReadInt(.settlement, "items/God's Gift") == 1)
			newunit.AddItem("God's Gift");
		if (EnvReadInt(.settlement, "items/Veteran Guild") == 1)
			newunit.AddItem("Veteran Guild");
			
		if(EnvReadString(.player, "Power of Seth") == "researched")
			if(newunit.AsUnit.stamina < 15)
				newunit.AsUnit.AddBonus(0,0,0,5,0);
	}
}

